package karileet.meeting.profile.receiver;

import karileet.meeting.profile.MeetingProfile;
import karileet.meeting.profile.ProfileConf;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.media.AudioManager;
import android.util.Log;

public class RingerModeStateChangeReceiver extends BroadcastReceiver {
	private static final String ACTION = "android.media.RINGER_MODE_CHANGED";
	
	@Override
	public void onReceive(Context context, Intent intent) {
		//first of all, determine whether this is ringer mode broadcast
		if (intent.getAction().equals(ACTION)) {
			Log.d(ProfileConf.APP, "Received ringer mode change");
			//now check what change actually took place
			//and how to handle it in application
			ProfileConf conf = new ProfileConf(context);
			MeetingProfile profile = new MeetingProfile(context);
			AudioManager am = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
			//sound mode is set to normal when profile is active!
			if (am.getRingerMode() == AudioManager.RINGER_MODE_NORMAL
				&& conf.isProfileEnabled()) {
				Log.d(ProfileConf.APP, "Mode is normal, disabling profile!");
				//disable profile
				profile.disable();
				//update widget image
				profile.updateWidget();
			}
			//sound mode is set to vibrate when profile is active and vibrate
			//mode is off
			if (am.getRingerMode() == AudioManager.RINGER_MODE_VIBRATE
				&& conf.isProfileEnabled() && !conf.isVibrateEnabled()) {
				Log.d(ProfileConf.APP, "Mode is vibrate, enabling vibrate");
				//enable vibrate mode
				conf.setVibrateEnabled(true);
			}
			
			//sound mode is set to silent when profile is active and vibrate
			//mode is on
			if (am.getRingerMode() == AudioManager.RINGER_MODE_SILENT
				&& conf.isProfileEnabled() && conf.isVibrateEnabled()) {
				Log.d(ProfileConf.APP, "Mode is silent, disabling vibrate");
				//disable vibrate mode
				conf.setVibrateEnabled(false);
			}
			
		}
	}

}
